package com.iotek.controller;

import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.iotek.entity.Address;
import com.iotek.entity.Business;
import com.iotek.entity.Customer;
import com.iotek.entity.Dispathing;
import com.iotek.entity.Order;
import com.iotek.service.IBusinessService;
import com.iotek.service.IPaymentService;
@Controller
@RequestMapping("/business")
public class BusinessController {
	@Autowired
	private IBusinessService businessService;
	@Autowired
	private IPaymentService payment;
	@RequestMapping("/selectOrder.action")
	public String selectsOrder(Order order,Model Model) {
		String orderavai = null;
		List<Order> list = businessService.selectOrder(order);
		if(!list.isEmpty()) {
			for(Order order2:list) {
				Model.addAttribute("order", order2);
				orderavai = order2.getOrderState();
			}
			if(orderavai.equals("已支付")) {
				return "paySuccess";
			}else {
				return "queryGoods1";
			}
		}else {
			return "queryGoods1";
		}
	}
	@RequestMapping("/selectMessage.action")
	public String selectMessage(@RequestParam("orderNumber")String ordernumber,Order order,Model Model) {
		order.setOrderNumber(ordernumber);
		List<Business> list=businessService.selectMessage(order);
		for(Business business:list) {
			Customer customer = business.getCustomer();
			Model.addAttribute("customer", customer);
			List<Order> list2  = business.getOrder();
			for(Order or:list2) {
				Model.addAttribute("order", or);
			}
			List<Address> add = business.getAddress();
			for(Address address:add) {
				Model.addAttribute("address", address);
			}
		}
		return "dispathing";
	}
	@RequestMapping("/insertBusiness.action")
	public String insertBusiness(HttpServletRequest req,Dispathing dispathing,Order order) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date = simpleDateFormat.format(System.currentTimeMillis());
		String num =  req.getParameter("orderNumber");
		String kuaidi = req.getParameter("kuaidi");
		String orderNumber = req.getParameter("orderNumber");
		order.setOrderNumber(orderNumber);
		payment.updateOrderState1(order);
		dispathing.setDispathingTime(date);
		dispathing.setDispathingState("已发货");
		dispathing.setOrderNumber(num);
		dispathing.setDispathingType(kuaidi);
		businessService.addDispathing(dispathing);
		return "success";
		
	}
	/**
	 * 商家注册
	 * @param request
	 * @param business
	 * @return
	 */
	@RequestMapping("/businessLogin.action")
	public String insertBusiness2(HttpServletRequest req,Business business,Model model){
		String businessPhone = req.getParameter("businessPhone");
		String password = req.getParameter("password");
		business.setBusinessPhone(businessPhone);
		Business bus = businessService.selectBusiness(business);
		if(bus!=null) {
			if(bus.getBusinessPhone().equals(businessPhone) && bus.getPassword().equals(password)) {
				model.addAttribute("businessName", bus.getBusinessName());
				return "business";
			}else {
				return "businessLogin";
			}
		}else {
			return "businessRegister";
		}
		
	}
	@RequestMapping("/insertBusiness1.action")
	public String insertBusiness(HttpServletRequest request,Business business) {
		String userPass = request.getParameter("password");
		String busPhone = request.getParameter("businessPhone");
		business.setPassword(userPass);
		business.setBusinessPhone(busPhone);
		businessService.insertBusiness(business);
		return "businessLogin";
	}

}

